package com.lineage.data.item_etcitem.add;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.C1_Name_Table;
import com.lineage.server.datatables.lock.CharacterC1Reading;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ChangeName;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1User_Power;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class chang_othertype extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(chang_othertype.class);
  
  public static ItemExecutor get() {
    return new chang_othertype();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int itemId = item.getItem().getItemId();
    try {
      if (pc == null)
        return; 
      if (pc.get_c_power() != null && pc.get_c_power().get_c1_type() == 0) {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("您尚未加入阵营!!"));
        return;
      } 
      if (itemId == 92104) {
        if (pc.get_c_power().get_c1_type() > 0) {
          pc.get_c_power().set_c1_type(0);
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("您成功脱离了阵营"));
          CharacterC1Reading.get().updateCharacterC1(pc.getId(), 0, "无");
          pc.get_c_power().set_power(pc, false);
          pc.sendPacketsAll((ServerBasePacket)new S_ChangeName(pc, true));
          pc.save();
        } 
        pc.get_c_power().set_c1_type(1);
        CharacterC1Reading.get().updateCharacterC1(pc.getId(), 1, "转换-复苏");
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("您以强制转入阵营国度-复苏"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("请登出在进来!!进行更新"));
        add_type(1, pc);
      } else if (itemId == 92105) {
        if (pc.get_c_power().get_c1_type() > 0) {
          pc.get_c_power().set_c1_type(0);
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("您成功脱离了阵营"));
          CharacterC1Reading.get().updateCharacterC1(pc.getId(), 0, "无");
          pc.get_c_power().set_power(pc, false);
          pc.sendPacketsAll((ServerBasePacket)new S_ChangeName(pc, true));
          pc.save();
        } 
        pc.get_c_power().set_c1_type(2);
        CharacterC1Reading.get().updateCharacterC1(pc.getId(), 2, "转换-勇猛");
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("您以强制转入阵营国度-勇猛"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("请登出在进来!!进行更新"));
        add_type2(2, pc);
      } else if (itemId == 92106) {
        if (pc.get_c_power().get_c1_type() > 0) {
          pc.get_c_power().set_c1_type(0);
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("您成功脱离了阵营"));
          CharacterC1Reading.get().updateCharacterC1(pc.getId(), 0, "无");
          pc.get_c_power().set_power(pc, false);
          pc.sendPacketsAll((ServerBasePacket)new S_ChangeName(pc, true));
          pc.save();
        } 
        pc.get_c_power().set_c1_type(3);
        CharacterC1Reading.get().updateCharacterC1(pc.getId(), 3, "转换-抵御");
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("您以强制转入阵营国度-抵御"));
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("请登出在进来!!进行更新"));
        add_type3(3, pc);
      } 
      pc.getInventory().removeItem(item, 1L);
      pc.save();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      _log.error("转换阵营设定异常", e);
    } 
  }
  
  private void add_type(int cmd, L1PcInstance pc) {
    int type = 1;
    String typeName = C1_Name_Table.get().get(1);
    if (pc.get_c_power() == null) {
      L1User_Power power = new L1User_Power();
      power.set_object_id(pc.getId());
      power.set_c1_type(1);
      power.set_note(typeName);
      pc.set_c_power(power);
      CharacterC1Reading.get().storeCharacterC1(pc);
    } else {
      pc.get_c_power().set_c1_type(1);
      pc.get_c_power().set_note(typeName);
      CharacterC1Reading.get().updateCharacterC1(pc.getId(), 1, typeName);
    } 
    pc.get_c_power().set_power(pc, false);
    pc.sendPacketsAll((ServerBasePacket)new S_ChangeName(pc, true));
    pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR成功加入阵营：" + typeName));
  }
  
  private void add_type2(int cmd, L1PcInstance pc) {
    int type = 2;
    String typeName = C1_Name_Table.get().get(2);
    if (pc.get_c_power() == null) {
      L1User_Power power = new L1User_Power();
      power.set_object_id(pc.getId());
      power.set_c1_type(2);
      power.set_note(typeName);
      pc.set_c_power(power);
      CharacterC1Reading.get().storeCharacterC1(pc);
    } else {
      pc.get_c_power().set_c1_type(2);
      pc.get_c_power().set_note(typeName);
      CharacterC1Reading.get().updateCharacterC1(pc.getId(), 2, typeName);
    } 
    pc.get_c_power().set_power(pc, false);
    pc.sendPacketsAll((ServerBasePacket)new S_ChangeName(pc, true));
    pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR成功加入阵营：" + typeName));
  }
  
  private void add_type3(int cmd, L1PcInstance pc) {
    int type = 3;
    String typeName = C1_Name_Table.get().get(3);
    if (pc.get_c_power() == null) {
      L1User_Power power = new L1User_Power();
      power.set_object_id(pc.getId());
      power.set_c1_type(3);
      power.set_note(typeName);
      pc.set_c_power(power);
      CharacterC1Reading.get().storeCharacterC1(pc);
    } else {
      pc.get_c_power().set_c1_type(3);
      pc.get_c_power().set_note(typeName);
      CharacterC1Reading.get().updateCharacterC1(pc.getId(), 3, typeName);
    } 
    pc.get_c_power().set_power(pc, false);
    pc.sendPacketsAll((ServerBasePacket)new S_ChangeName(pc, true));
    pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR成功加入阵营：" + typeName));
  }
}
